﻿using System;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x020002CD RID: 717
public class PoseHandle : MonoBehaviour
{
	// Token: 0x060010C0 RID: 4288 RVA: 0x0007A74C File Offset: 0x0007894C
	public void SetEffector(IKEffector effector, Transform parent, float scale)
	{
		this.SetScale(scale);
		this.effector = effector;
		this.bend = null;
		base.gameObject.SetActive(true);
		this.update = true;
		this.nextUpdate = Time.time + 0.3f;
		base.transform.SetParent(parent);
	}

	// Token: 0x060010C1 RID: 4289 RVA: 0x0007A7A0 File Offset: 0x000789A0
	public void SetBendGoal(IKConstraintBend bend, Transform parent, float scale)
	{
		this.SetScale(scale);
		this.bend = bend;
		this.effector = null;
		if (this.line == null)
		{
			this.line = base.gameObject.AddComponent<LineRenderer>();
		}
		this.line.positionCount = 2;
		this.line.material = base.GetComponentInChildren<Renderer>().material;
		this.line.startColor = Color.yellow;
		this.line.endColor = Color.yellow;
		this.line.widthMultiplier = 5f;
		base.gameObject.SetActive(true);
		this.update = true;
		this.nextUpdate = Time.time + 0.3f;
		base.transform.SetParent(parent);
	}

	// Token: 0x060010C2 RID: 4290 RVA: 0x0007A866 File Offset: 0x00078A66
	private void SetScale(float scale)
	{
		base.transform.SetParent(null);
		base.transform.localScale = new Vector3(scale, scale, scale);
	}

	// Token: 0x060010C3 RID: 4291 RVA: 0x0007A888 File Offset: 0x00078A88
	public void SetLookAt(IKSolverLookAt lookAt, Transform parent, float scale)
	{
		this.SetScale(scale);
		this.lookAt = lookAt;
		if (this.line == null)
		{
			this.line = base.gameObject.AddComponent<LineRenderer>();
		}
		this.line.positionCount = 2;
		this.line.material = base.GetComponentInChildren<Renderer>().material;
		this.line.startColor = Color.yellow;
		this.line.endColor = Color.yellow;
		this.line.widthMultiplier = 5f;
		base.gameObject.SetActive(true);
		this.update = true;
		this.nextUpdate = Time.time + 0.3f;
		base.transform.SetParent(parent);
	}

	// Token: 0x060010C4 RID: 4292 RVA: 0x0007A948 File Offset: 0x00078B48
	public void UpdatePosition()
	{
		if (this.effector != null)
		{
			base.transform.position = this.effector.position;
			base.transform.rotation = this.effector.rotation;
		}
		else if (this.bend != null)
		{
			base.transform.position = this.bend.bendGoal.position;
			base.transform.rotation = this.bend.bendGoal.rotation;
		}
		else if (this.lookAt != null)
		{
			base.transform.position = this.lookAt.IKPosition;
		}
	}

	// Token: 0x060010C5 RID: 4293 RVA: 0x0007A9F8 File Offset: 0x00078BF8
	private void Update()
	{
		if (this.effector == null && this.bend == null && this.lookAt == null)
		{
			base.gameObject.SetActive(false);
			return;
		}
		if (this.update && Time.time > this.nextUpdate)
		{
			this.UpdatePosition();
			this.update = false;
		}
		else if (this.bend != null)
		{
			this.originPosition = this.bend.bone2.position;
		}
		else if (this.lookAt != null)
		{
			this.originPosition = this.lookAt.head.transform.position;
		}
	}

	// Token: 0x060010C6 RID: 4294 RVA: 0x0007AAAC File Offset: 0x00078CAC
	private void LateUpdate()
	{
		if (this.update)
		{
			return;
		}
		if (this.effector != null)
		{
			this.effector.position = base.transform.position;
			this.effector.rotation = base.transform.rotation;
		}
		else if (this.bend != null)
		{
			this.bend.bendGoal.position = base.transform.position;
			this.bend.bendGoal.rotation = base.transform.rotation;
			this.line.widthMultiplier = 5f * base.transform.lossyScale.y;
			this.line.SetPosition(0, this.originPosition);
			this.line.SetPosition(1, this.bend.bendGoal.position);
		}
		else if (this.lookAt != null)
		{
			this.lookAt.IKPosition = base.transform.position;
			this.line.widthMultiplier = 5f * base.transform.lossyScale.y;
			this.line.SetPosition(0, this.originPosition);
			this.line.SetPosition(1, this.lookAt.IKPosition);
		}
	}

	// Token: 0x04001580 RID: 5504
	private IKEffector effector;

	// Token: 0x04001581 RID: 5505
	private IKConstraintBend bend;

	// Token: 0x04001582 RID: 5506
	private IKSolverLookAt lookAt;

	// Token: 0x04001583 RID: 5507
	private bool update;

	// Token: 0x04001584 RID: 5508
	private float nextUpdate;

	// Token: 0x04001585 RID: 5509
	private LineRenderer line;

	// Token: 0x04001586 RID: 5510
	private Vector3 originPosition = Vector3.zero;
}
